#include "config.h"

Config::Config()
{
	std::fstream file("config.txt");

	std::string name; // Variable name
	std::string val; // Variable value

	bool done = false;
	while(!done) {
		name.erase(); val.erase();

		getline(file,name,'=');
		getline(file,val);

		if ("port" == name) this->c_port = static_cast<unsigned short>(atoi(val.c_str()));
	    else if ("max users" == name) this->c_max_players = static_cast<unsigned char>(atoi(val.c_str()));
		else if ("name" == name) this->c_name = val;
		else if ("motd" == name) this->c_motd = val;
		else if ("public" == name) this->c_public = val;
		else if ("verify names" == name) {
			if ("false" == val) this->c_verify_names = false;
			else this->c_verify_names = true;
		}
		else if ("map name" == name) this->c_map_name = val;
		else if ("upload" == name) this->c_upload =  static_cast<int>(atoi(val.c_str()));

		if (file.eof()) done = true;
	}
	file.close();

	file.open("admins.txt");
	file.clear();
	while( !file.eof()) {
		getline(file,name);
		this->admins.insert(name);
	}	
	file.close();
	
	file.open("bans.txt");
	file.clear();
	while( !file.eof()) {
		getline(file,name);
		this->bans.insert(name);
	}
	file.close();
}

int Config::add_admin(std::string name)
{
	if (this->is_admin(name) == 0) {	
		this->admins.insert(name);
		return this->save_admins();
	}
	return 0;
}

int Config::del_admin(std::string name)
{
	if (this->is_admin(name) == 1) {	
		this->admins.erase(name);
		return this->save_admins();
	}
	return 0;
}

int Config::save_admins()
{
	std::ofstream file("admins.txt", std::ios::trunc);
	if (file == NULL) {
		printf("Failed to open \"admins.txt\".\n");
		return 1;
	}
	for (std::set<std::string>::iterator i = this->admins.begin(); i != this->admins.end(); i++) {
		char buffer[1024];
		memcpy(buffer, i->data(), i->length());
		buffer[i->length()] = '\n';
		file.write(buffer, i->length()+1);
	}
	file.close();
	return 0;
}

int Config::add_ban(std::string name)
{
	if (this->is_banned(name) == 0) {	
		this->bans.insert(name);
		return this->save_bans();
	}
	return 0;
}

int Config::del_ban(std::string name)
{
	if (this->is_banned(name) == 1) {	
		this->bans.erase(name);
		return this->save_bans();
	}
	return 0;
}

int Config::save_bans()
{
	std::ofstream file("bans.txt", std::ios::trunc);
	if (file == NULL) {
		printf("Failed to open \"bans.txt\".\n");
		return 1;
	}
	for (std::set<std::string>::iterator i = this->bans.begin(); i != this->bans.end(); i++) {
		char buffer[1024];
		memcpy(buffer, i->data(), i->length());
		buffer[i->length()] = '\n';
		file.write(buffer, i->length()+1);
	}
	file.close();
	return 0;
}
	
unsigned short Config::port()
{
	return this->c_port;
}

unsigned char Config::max_players()
{
	return this->c_max_players;
}

std::string Config::name()
{
	return this->c_name;
}

std::string Config::motd()
{
	return this->c_motd;
}

bool Config::is_admin(std::string str)
{
	return this->admins.count(str);
}

bool Config::is_banned(std::string str)
{
	return this->bans.count(str);
}

std::string Config::map_name()
{
	return this->c_map_name;
}

std::string Config::is_public()
{
	return this->c_public;
}

int Config::upload()
{
	return this->c_upload;
}
